<?php

/**
 * producto actions.
 *
 * @package    administracion-ventas
 * @subpackage producto
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class productoActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
    public function executeList(sfWebRequest $request)
    {
        $crit = new Criteria();
        if ($request->isMethod('get'))
        {
            $campos = $request->getGetParameters();
            if (isset($campos['nombre']) && !empty($campos['nombre']))
            $crit->add(ProductoPeer::NOMBRE, "%".$request->getParameter('nombre')."%", Criteria::LIKE);

            if (isset($campos['precio']) && !empty($campos['precio']))
            $crit->add(ProductoPeer::PRECIO, (float)$request->getParameter('precio'));

            if (isset($campos['codigo']) && !empty($campos['codigo']))
            $crit->add(ProductoPeer::CODIGO, $request->getParameter('codigo'));
            if ($request->getParameter('order'))
            if ($request->getParameter('type') == 'asc')
                    $crit->addAscendingOrderByColumn(constant('ProductoPeer::'.strtoupper($request->getParameter('order'))));
                else
                    $crit->addDescendingOrderByColumn(constant('ProductoPeer::'.strtoupper($request->getParameter('order'))));
        }

        $this->pager = ProductoPeer::getProductosPager($crit, $request->getParameter('page', 1), $peerMethod = null, $peerCountMethod = null);
    }

    public function executeAjax(sfWebRequest $request)
    {
        $this->getResponse()->setContentType('application/json');

        $productos = ProductoPeer::retrieveForAutocompleter($request->getParameter('q'), $request->getParameter('limit'));
        return $this->renderText(json_encode($productos));

    }

    public function executeGetPrice(sfWebRequest $request)
    {
        sfContext::getInstance()->getConfiguration()->loadHelpers(array('Form', 'Tag'));
        $prod = ProductoPeer::retrieveByPk($request->getParameter('id'));
        if (!$prod)
        return $this->renderText("$".input_tag('venta[linea_venta]['.$request->getParameter('fila').'][precio_producto]', '', array('size' => '6', 'maxlength' => '7', 'readonly' => true)));

        return $this->renderText("$".input_tag('venta[linea_venta]['.$request->getParameter('fila').'][precio_producto]', $prod->getPrecio(), array('size' => '6', 'maxlength' => '7', 'readonly' => true)));
    }
}
